package com.dimples.dd.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.system.convert.Oauth2AuthorizationConvert;
import com.dimples.dd.system.mapper.Oauth2AuthorizationMapper;
import com.dimples.dd.system.model.entity.Oauth2AuthorizationDO;
import com.dimples.dd.system.model.query.OAuth2AccessTokenPageQuery;
import com.dimples.dd.system.model.vo.OAuth2AccessTokenPageVO;
import com.dimples.dd.system.service.Oauth2AuthorizationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class Oauth2AuthorizationServiceImpl extends ServiceImpl<Oauth2AuthorizationMapper, Oauth2AuthorizationDO> implements Oauth2AuthorizationService {

    private final Oauth2AuthorizationConvert oauth2AuthorizationConvert;

    @Override
    public PageResult<OAuth2AccessTokenPageVO> selectAccessTokenPage(OAuth2AccessTokenPageQuery pageQuery) {
        PageResult<Oauth2AuthorizationDO> doPageResult = this.baseMapper.selectPage(pageQuery);
        Map<String, Oauth2AuthorizationDO> authorizationDoMap = doPageResult.getData().getRecords().stream().collect(Collectors.toMap(Oauth2AuthorizationDO::getId, item -> item));

        PageResult<OAuth2AccessTokenPageVO> voPageResult = this.oauth2AuthorizationConvert.entity2Page(doPageResult);
        voPageResult.getData().getRecords().forEach(item -> {
            Oauth2AuthorizationDO authorizationDO = authorizationDoMap.get(item.getId());
            item.setRefreshToken(StrUtil.str(authorizationDO.getRefreshTokenValue(), StandardCharsets.UTF_8));

            if (ObjUtil.isNotEmpty(authorizationDO.getAuthorizationCodeValue())) {
                item.setAccessToken(StrUtil.str(authorizationDO.getAuthorizationCodeValue(), StandardCharsets.UTF_8));
                item.setAccessTokenIssuedAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getAuthorizationCodeIssuedAt())));
                item.setAccessTokenExpiresAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getAuthorizationCodeExpiresAt())));
            } else if (ObjUtil.isNotEmpty(authorizationDO.getAccessTokenValue())) {
                item.setAccessToken(StrUtil.str(authorizationDO.getAccessTokenValue(), StandardCharsets.UTF_8));
                item.setAccessTokenIssuedAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getAccessTokenIssuedAt())));
                item.setAccessTokenExpiresAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getAccessTokenExpiresAt())));
            } else if (ObjUtil.isNotEmpty(authorizationDO.getDeviceCodeValue())) {
                item.setAccessToken(StrUtil.str(authorizationDO.getDeviceCodeValue(), StandardCharsets.UTF_8));
                item.setAccessTokenIssuedAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getDeviceCodeIssuedAt())));
                item.setAccessTokenExpiresAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getDeviceCodeExpiresAt())));
            } else if (ObjUtil.isNotEmpty(authorizationDO.getOidcIdTokenValue())) {
                item.setAccessToken(StrUtil.str(authorizationDO.getOidcIdTokenValue(), StandardCharsets.UTF_8));
                item.setAccessTokenIssuedAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getOidcIdTokenIssuedAt())));
                item.setAccessTokenExpiresAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getOidcIdTokenExpiresAt())));
            } else if (ObjUtil.isNotEmpty(authorizationDO.getUserCodeValue())) {
                item.setAccessToken(StrUtil.str(authorizationDO.getUserCodeValue(), StandardCharsets.UTF_8));
                item.setAccessTokenIssuedAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getUserCodeIssuedAt())));
                item.setAccessTokenExpiresAt(DateUtil.parseLocalDateTime(DateUtil.formatDateTime(authorizationDO.getUserCodeExpiresAt())));
            }
        });

        return voPageResult;
    }
}
